package cn.tedu.tmall.front.mall.controller;

import cn.tedu.tmall.common.pojo.authentication.CurrentPrincipal;
import cn.tedu.tmall.common.web.JsonResult;
import cn.tedu.tmall.front.mall.service.ICartService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

@RestController
@RequestMapping("/carts")
@Validated
@Api(tags = "05. 购物车管理")
public class CartController {

    @Autowired
    private ICartService cartService;

    @PostMapping("/add")
    @ApiOperation("添加商品到购物车")
    @ApiOperationSupport(order = 100)
    public JsonResult add(@AuthenticationPrincipal @ApiIgnore CurrentPrincipal currentPrincipal,
                          @Range(min = 1, message = "请提交合法的商品ID值") Long goodsId,
                          @Range(min = 1, max = 100, message = "商品数量必须是1~100之间!") Integer goodsNum) {
        cartService.add(currentPrincipal, goodsId, goodsNum);
        return JsonResult.ok();
    }

    @PostMapping("/goods-num/increase")
    @ApiOperation("将购物车中商品的数量增加1")
    @ApiOperationSupport(order = 310)
    public JsonResult increaseNum(@AuthenticationPrincipal @ApiIgnore CurrentPrincipal currentPrincipal,
                                  @Range(min = 1, message = "请提交合法的商品ID值！") Long goodsId) {
        Integer newNum = cartService.increaseNum(currentPrincipal, goodsId);
        return JsonResult.ok(newNum);
    }

    @PostMapping("/goods-num/increase-num")
    @ApiOperation("增加购物车中商品的数量")
    @ApiOperationSupport(order = 311)
    public JsonResult increaseNum(@AuthenticationPrincipal @ApiIgnore CurrentPrincipal currentPrincipal,
                                  @Range(min = 1, message = "请提交合法的商品ID值！") Long goodsId,
                                  @Range(min = 1, max = 100, message = "商品数量必须是1~100之间！") Integer num) {
        Integer newNum = cartService.increaseNum(currentPrincipal, goodsId, num);
        return JsonResult.ok(newNum);
    }

    @PostMapping("/goods-num/reduce")
    @ApiOperation("将购物车中商品的数量减少1")
    @ApiOperationSupport(order = 320)
    public JsonResult reduceNum(@AuthenticationPrincipal @ApiIgnore CurrentPrincipal currentPrincipal,
                                @Range(min = 1, message = "请提交合法的商品ID值！") Long goodsId) {
        Integer newNum = cartService.reduceNum(currentPrincipal, goodsId);
        return JsonResult.ok(newNum);
    }

    @PostMapping("/goods-num/reduce-num")
    @ApiOperation("减少购物车中商品的数量")
    @ApiOperationSupport(order = 321)
    public JsonResult reduceNum(@AuthenticationPrincipal @ApiIgnore CurrentPrincipal currentPrincipal,
                                @Range(min = 1, message = "请提交合法的商品ID值！") Long goodsId,
                                @Range(min = 1, max = 100, message = "商品数量必须是1~100之间！") Integer num) {
        Integer newNum = cartService.reduceNum(currentPrincipal, goodsId, num);
        return JsonResult.ok(newNum);
    }


}
